<?php
/**
 * Created by PhpStorm.
 * FileName: Appointment.class.php
 * User: Administrator
 * Date: 2018/4/16
 * Time: 10:02
 */

namespace Api\Controller;

use Admin\Model\MoneyModel;
use Admin\Model\MoneyOutModel;
use Admin\Model\UserModel;

class MoneyController extends ApiController {

    private $moneyModel;
    private $moneyOutModel;
    private $userModel;

    public function __construct() {
        parent::__construct();
        $this->moneyModel = new MoneyModel();
        $this->moneyOutModel = new MoneyOutModel();
        $this->userModel = new UserModel();
    }

    // 我的提现列表
    public function myList() {
        $where = array();
        $user_id = I('user_id');
        if (!$user_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $where['user_id'] = $user_id;
        $pageSize = 15;
        $curPage = I('p',1);
        $data = $this->moneyOutModel->getList($where,$pageSize,$curPage);
        return $this->callBack("提现列表",$this->SUCCESS_CODE,$data);
    }

    // 申请提现
    public function create() {
        $data = array();
        $user_id = I('user_id');
        $money = I('money');
        //$account = I('account');
        if (!$user_id || !$money) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $moneyOutMin = $this->sysConfig['money_out_min'];
        if ($money<$moneyOutMin) {
            $this->callBack('提现金额必须大于 '.$moneyOutMin.' 元',$this->ERROR_CODE);
        }
        $openId = $this->userModel->where(array('id' => $user_id))->getField('openid');
        $data['user_id'] = $user_id;
        $data['money'] = $money;
        //$data['account'] = $account;
        $data['openid'] = $openId;
        $rs = $this->moneyOutModel->add($data);
        if ($rs !== false) {
            //$this->handleWXNotice($rs);
            return $this->callBack('提现申请成功！',$this->SUCCESS_CODE);
        }
        return $this->callBack('提现申请失败！',$this->ERROR_CODE);
    }

    // 退款列表
    public function refund() {
        $user_id = I('user_id');
        if (!$user_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $where = array('type' => 3);
        $where['user_id'] = $user_id;
        $pageSize = 15;
        $curPage = I('p',1);
        $data = $this->moneyModel->getList($where,$pageSize,$curPage);
        return $this->callBack("退款列表",$this->SUCCESS_CODE,$data);
    }

    public function capitalFlow () {
        $user_id = I('user_id');
        if (!$user_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $pageSize = 15;
        $curPage = I('p',1);
        $where = array('user_id' => $user_id);
        $data = $this->moneyModel->getList($where,$pageSize,$curPage);
        return $this->callBack("资金流水列表",$this->SUCCESS_CODE,$data);
    }

    private function handleWXNotice($id) {
        $templateId = C('WEIXIN_TEMPLATE_MONEY_CREATE');
        $row = $this->moneyOutModel->where(array('id' => $id))->find();
        $user = $this->userModel->where(array('id' => $row['user_id']))->find();
        $title = '提现通知！';
        $option = array(
        );
        $options = array(
            'templateId' => $templateId,
            'moneyOutId' => $id,
            'openId' => $user['openid'],
            'data' => $option,
            'title' => $title
        );
        return $this->sendNotice($options);
    }

    //发送微信通知
    private function sendNotice($options) {
        $option = array(
            //接收者openid
            'touser' => $options['openId'],
            //模板ID
            'template_id' => $options['templateId'],
            //模板跳转链接，不填则不用跳转
            'url' => '',
            //模板数据
            'data' => $options['data']
        );
        $response = sendWenXinNotice($option);
        $result = $response['result'];
        return $result;
    }

}